/*
 *  sender.c
 *  FireWall
 *
 *  Created by Admin on 8/01/13.
 *  Copyright (c) 2013 Admin. All rights reserved.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/kern_control.h>
#include <sys/sys_domain.h>

#define MAX_STRING_LEN 255

int main(int argc, char* const*argv)
{
    struct ctl_info ctl_info;
    struct sockaddr_ctl sc;
    char str[MAX_STRING_LEN];
    int sock = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
    if (sock < 0)
        return -1;
    
    bzero(&ctl_info, sizeof(struct ctl_info));
    strcpy(ctl_info.ctl_name, "com.osxkernel.HelloKernControl");
    
    if (ioctl(sock, CTLIOCGINFO, &ctl_info) == -1){
        printf("can't ioctl\n");
        return -1;
    }
    
    bzero(&sc, sizeof(struct sockaddr_ctl));
    sc.sc_len = sizeof(struct sockaddr_ctl);
    sc.sc_family = AF_SYSTEM;
    sc.ss_sysaddr = SYSPROTO_CONTROL;
    sc.sc_id = ctl_info.ctl_id;
    sc.sc_unit = 0;
    
    if (connect(sock, (struct sockaddr *)&sc, sizeof(struct sockaddr_ctl))){
        
        printf("can't sock\n");
        return -1;
    }
    
    strcpy(str, argv[1]);
    switch(*argv[2]){
        case 115:
            if (setsockopt(sock, SYSPROTO_CONTROL, 2,
                           str, (socklen_t)strlen(str)) == -1)
                return  0;
            close(sock);
            break;
        case 100:
            if (setsockopt(sock, SYSPROTO_CONTROL, 3,
                           str, (socklen_t)strlen(str)) == -1)
                return 0 ;
            close(sock);
            break;
        default: printf("bad index\n");
    }
    return 0;
}
